package com.prs.crm.action.fs;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.scheduling.quartz.AdaptableJobFactory;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.DirectEntry;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.fs.DirectEntryService;

public class DirectEntryAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private DirectEntry directEntry;
	private DirectEntryService directEntryService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private BankAccountService bankAccountService;
	private DetailService detailService;

	private Collection<BankAccount> bankAccounts;
	private File upload;
	private String uploadFileName;
	private String bank;

	@Action("listDirectEntry")
	public String listDirectEntry() {
		return SUCCESS;
	}

	@Action("listDirectEntryByBank")
	public String listDirectEntryByBank() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DirectEntry.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if (bank != null) {
			if (!"all".equals(bank)) {
				bank = bank.split(",")[0];
				if (ToolKits.isInteger(bank)) {
					criteria.createAlias("bankAccount", "ba");
					criteria.add(Restrictions.eq("ba.id", Integer
							.parseInt(bank)));
				}
			}
		}
		this.setPagination(this.getDirectEntryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("alterDirectEntry")
	public String alterDirectEntry() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DirectEntry.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDirectEntryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editDirectEntry")
	public String editDirectEntry() {
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitDirectEntry", results = {
			@Result(name = "success", type = "redirect", location = "editDirectEntry"),
			@Result(name = "error", type = "chain", location = "editDirectEntry") })
	public String submitDirectEntry() {
		if (directEntry.getTitle() == null
				|| "".equals(directEntry.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (directEntry.getBankAccount() == null) {
			addActionError("入账账户不能为空");
			return ERROR;
		}
		if (directEntry.getEntryTime() == null) {
			addActionError("入帐时间不能为空");
			return ERROR;
		}
		if (directEntry.getMoney() == null) {
			addActionError("入账金额不能为空");
			return ERROR;
		}
		if (directEntry.getHandler() == null) {
			addActionError("入账人不能为空");
			return ERROR;
		}
		this.getDirectEntryService().submitDirectEntry(directEntry, upload,
				uploadFileName);
		this.setPrompt("提交直接入账成功");
		return SUCCESS;
	}

	@Action(value = "removeDirectEntry", results = {
			@Result(name = "success", type = "redirect", location = "alterDirectEntry"),
			@Result(name = "error", type = "chain", location = "alterDirectEntry") })
	public String removeDirectEntry() {
		if (ids == null) {
			this.addActionError("请选择要删除的直接入账单");
			return ERROR;
		}
		this.getDirectEntryService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("viewDirectEntry")
	public String viewDirectEntry() {
		return SUCCESS;
	}

	public DirectEntry getDirectEntry() {
		return directEntry;
	}

	public void setDirectEntry(DirectEntry directEntry) {
		this.directEntry = directEntry;
	}

	public DirectEntryService getDirectEntryService() {
		return directEntryService;
	}

	public void setDirectEntryService(DirectEntryService directEntryService) {
		this.directEntryService = directEntryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}

}
